﻿using KeZhou.ViewModels;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace KeZhou.Views.Components
{
    /// <summary>
    /// ResetPasswordControl.xaml 的交互逻辑
    /// </summary>
    public partial class ResetPasswordControl : UserControl
    {
        //切换控件使用的事件
        public delegate void SwitchControlEventHandler(object sender, ControlType controlType);
        public event SwitchControlEventHandler SwitchControl;
        //60s发送一次验证码需要的计时器
        Timer timer = new Timer(1000);
        int sec = 0;

        public ResetPasswordControl()
        {
            InitializeComponent();
            //绑定数据
            this.DataContext = ResetPasswordViewModel.Instance;
            //绑定计时器
            timer.Elapsed += TimerElapsed;
        }

        /*
         * 切换控件
         */
        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            SwitchControl?.Invoke(this, ControlType.Login);
        }

        /*
         * 按钮事件-获取验证码，在View层初步验证手机格式
         */
        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.MainWindow is MainWindow mainWindow)
            {
                if (Regex.IsMatch(rpPhone.Text, @"^\d{11}$"))
                {
                    if (PhoneCodeViewModel.Instance.getCodeByPhone(rpPhone.Text,1))
                    {
                        sixtySec();
                    }
                    else
                    {
                        //后端发送验证码失效
                        mainWindow.showWrongMsg(1);
                    }
                }
                else
                {
                    //手机号格式错误
                    mainWindow.showWrongMsg(0);
                }
            }
        }

        //60s倒计时
        private void sixtySec()
        {
            sec = 60;
            timer.Start();
            Dispatcher.Invoke(() => getCodeTB.IsHitTestVisible = false);
        }

        //timer开始时每秒钟触发
        private void TimerElapsed(object sender, ElapsedEventArgs e)
        {
            if (sec > 0)
            {
                Dispatcher.Invoke(() => getCodeTB.Text = sec + "秒后可重发");
                sec -= 1;
            }
            else
            {
                timer.Stop();
                Dispatcher.Invoke(() => {
                    getCodeTB.Text = "点击获取短信验证码";
                    getCodeTB.IsHitTestVisible = true;
                });
            }
        }

        /*
         * 按钮事件-重设密码，在View层初步验证格式和是否为空
         */
        private void Border_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            if (Application.Current.MainWindow is MainWindow mainWindow)
            {
                if (!(string.IsNullOrEmpty(rpPhone.Text) || string.IsNullOrEmpty(rpPassword1.Text) || string.IsNullOrEmpty(rpPassword2.Text) || string.IsNullOrEmpty(rpCode.Text)))
                {
                    if (rpPassword1.Text.Equals(rpPassword2.Text))
                    {
                        mainWindow.showWrongMsg(ResetPasswordViewModel.Instance.resetPassword());
                    }
                    else
                    {
                        //两次输入密码不一致
                        mainWindow.showWrongMsg(3);
                    }
                }
                else
                {
                    //输入的账号密码不完整
                    mainWindow.showWrongMsg(2);
                }
            }
        }
    }
}
